
// Copyright (C) Recursion Company. All rights reserved.

#include "stdafx.h"
#include "vol_user_app_info.h"

void CVWnd_HyperLink::OnConstruct ()
{
	m_nType = 1;
	m_strEmail.Empty ();
	m_strURL.Empty ();
	m_clrLink = RGB (0, 0, 255);
	m_clrVisited = RGB (128, 0, 0);
	m_clrHover = ::GetSysColor (COLOR_HIGHLIGHT);
	m_clrBack = CLR_DEFAULT;

	m_blOverControl = FALSE;
	m_bVisited = FALSE;
}

BOOL CVWnd_HyperLink::CreateControl (CVolNativeWndCreateParam* pCreateParam)
{
    pCreateParam->m_dwStyle &= ~WS_TABSTOP;

    return CWnd::CreateEx (pCreateParam->m_dwExStyle, VolRegisterWndClass (_T ("VolHyperLink"), NULL, IDC_HAND),
            pCreateParam->m_szCaption, pCreateParam->m_dwStyle,
            pCreateParam->m_nLeft, pCreateParam->m_nTop, pCreateParam->m_nWidth, pCreateParam->m_nHeight,
            pCreateParam->m_pParentWnd->GetSafeHwnd (), (HMENU)pCreateParam->m_nID);
}

BEGIN_MESSAGE_MAP(CVWnd_HyperLink, CWnd)
	//{{AFX_MSG_MAP(CVWnd_HyperLink)
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_PAINT()
    ON_WM_SETFONT ()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CVWnd_HyperLink::OnSetFont (CFont* pFont, BOOL bRedraw)
{
    LOGFONT infFont;

    if (pFont != NULL && pFont->GetLogFont (&infFont))
    {
        m_fntDraw.DeleteObject ();
        m_fntDraw.CreateFontIndirect (&infFont);

        if (bRedraw)
            Invalidate (FALSE);
    }
}

void CVWnd_HyperLink::OnLButtonDown(UINT nFlags, CPoint point) 
{
    m_bVisited = GotoURL ();

    if (m_bVisited)
        Invalidate (FALSE); 
}

void CVWnd_HyperLink::OnMouseMove(UINT nFlags, CPoint point) 
{
    if (m_blOverControl == TRUE)
    {
        CRect rect;
        GetClientRect (rect);

        if (!rect.PtInRect (point))
        {
            m_blOverControl = FALSE;
            ReleaseCapture ();
	        Invalidate (FALSE);
        }
		else if (::GetCapture () != m_hWnd)
			SetCapture ();
    }
    else
    {
        m_blOverControl = TRUE;
        Invalidate (FALSE);
        SetCapture ();
    }
}

void CVWnd_HyperLink::OnPaint() 
{
	CPaintDC dc (this);

	CRect rtClipBox;
	dc.GetClipBox (&rtClipBox);
	if (rtClipBox.IsRectEmpty () == TRUE)
		return;

	CBrush brhBack (GetRealBackColor (m_clrBack));
	CBrush* pOldBrush = dc.SelectObject (&brhBack);
	dc.PatBlt (rtClipBox.left, rtClipBox.top, rtClipBox.Width (), rtClipBox.Height (), PATCOPY);
	dc.SelectObject (pOldBrush);

	CString str;
	GetWindowText (str);
	if (str.IsEmpty ())
		str = (m_nType == 0 ? m_strEmail.GetText () : m_strURL.GetText ());

	dc.SetBkMode (TRANSPARENT);
	dc.SetTextColor (m_blOverControl ? m_clrHover :
            m_bVisited ? m_clrVisited : m_clrLink);

    CFont* pOldFont = dc.GetCurrentFont ();
	if (m_fntDraw.m_hObject != NULL)
		dc.SelectObject (&m_fntDraw);
	else
		dc.SelectStockObject (DEFAULT_GUI_FONT);

	CRect rtClient;
	GetClientRect (&rtClient);
	dc.DrawText (str, &rtClient, (DT_NOPREFIX | DT_VCENTER | DT_SINGLELINE | DT_CENTER | DT_NOCLIP));

	dc.SelectObject (pOldFont);
}

BOOL CVWnd_HyperLink::GotoURL ()
{
    const CVolString& strURL = (m_nType == 0 ? m_strEmail : m_strURL);
    if (strURL.IsEmpty ())
        return FALSE;

    if (m_pEventListener != NULL && m_pEventListener->OnGoto (strURL.GetText ()))
        return TRUE;

	if (m_nType == 0)
        return (BOOL)OpenURL ((_T ("mailto:") + strURL).GetText (), SW_SHOW);

    return (BOOL)OpenURL (strURL.GetText (), SW_SHOW);
}
